/*
 *    Copyright 2009-2010 The iBaGuice Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.ibaguice.samples.multipledatasources;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.PrivateModule;
import com.google.inject.name.Names;
import com.googlecode.ibaguice.core.SqlSessionFactoryModule;
import com.googlecode.ibaguice.core.datasource.PooledDataSourceProvider;
import com.googlecode.ibaguice.samples.Contact;
import com.googlecode.ibaguice.samples.ContactDao;
import com.googlecode.ibaguice.samples.ContactMapper;
import com.googlecode.ibaguice.samples.TransactionalContactDao;

/**
 * 
 *
 * @version $Id: MultipleDataSourcesTestCase.java 2121 2010-06-16 09:15:12Z simone.tripodi $
 */
public final class MultipleDataSourcesTestCase {

    @Inject
    private ContactDao contactDao;

    @Inject
    private TradeDao tradeDao;

    public void setContactDao(ContactDao contactDao) {
        this.contactDao = contactDao;
    }

    public void setTradeDao(TradeDao tradeDao) {
        this.tradeDao = tradeDao;
    }

    @BeforeClass
    public void setUp() throws Exception {
        Injector injector = Guice.createInjector(new PrivateModule() {
            @Override
            protected void configure() {
                install(new SqlSessionFactoryModule(PooledDataSourceProvider.class)
                    .addSimpleAliases(Contact.class)
                    .addMapperClasses(ContactMapper.class));
                bind(ContactDao.class).to(TransactionalContactDao.class).asEagerSingleton();
                Names.bindProperties(this.binder(), getConnectionProperties("contacts"));
                expose(ContactDao.class);
            }
        }, new PrivateModule() {
            @Override
            protected void configure() {
                install(new SqlSessionFactoryModule(PooledDataSourceProvider.class)
                    .addSimpleAliases(Trade.class)
                    .addMapperClasses(TradeMapper.class));
                Names.bindProperties(this.binder(), getConnectionProperties("trades"));
                bind(TradeDao.class);
                expose(TradeDao.class);
            }
        });
        injector.injectMembers(this);
    }

    @Test
    public void verifyInjections() {
        assert this.contactDao != null;
        assert this.tradeDao != null;
    }

    private final static Properties getConnectionProperties(String dataSource) {
        // db URL setup
        File tmp = null;
        try {
            tmp = File.createTempFile(dataSource, ".dat");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        tmp.delete();
        final String connectionURL = "jdbc:derby:"
            + tmp.getAbsolutePath()
            + ";create=true";

        final Properties myBatisProperties = new Properties();
        myBatisProperties.setProperty("mybatis.environment.id", "test");
        myBatisProperties.setProperty("JDBC.driver", "org.apache.derby.jdbc.EmbeddedDriver");
        myBatisProperties.setProperty("JDBC.url", connectionURL);
        myBatisProperties.setProperty("JDBC.username", "");
        myBatisProperties.setProperty("JDBC.password", "");
        myBatisProperties.setProperty("JDBC.autoCommit", "true");

        return myBatisProperties;
    }

}
